(:
  A procedure to select the contents of coffee threaded chat interventions
  across several versions (alpha+, 1.6 and 1.7?, 2.0).
  
  all items contain infos:
  	software
  	tool
  	step
  	time
  	src-anchor
  	group
  	user
  	type
  	content
:)

declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
import module namespace
    coffee = "http://tatiana.emse.fr/coffee"
    at "coffee-utils.xq" ;
    
declare function local:format-category-name($name as xs:string*) as xs:string* {
	fn:substring-before(fn:substring-after($name,"("),")")
};

declare function local:format-tag-name($tag as xs:string*) as xs:string* {
        if (fn:substring($tag,1,5) eq "lead.") then
		fn:substring-before(fn:substring-after($tag,"strTag="),";")
	else
		$tag
};

<display>{

let $t := $arguments[1]
let $doc := doc($t)
let $version := coffee:get-version($doc)
let $msgs := coffee:get-messages($doc)
let $newmsgs := $msgs[.//(MLThreadedChatMessage|MLThreadedChatServerMessage|MLTCAddCategoryServerMessage|MLTCAddCategoryMessage)]
for $msg at $p in $msgs
let $vote := $msg//PositionometerMessage
let $startvote := $msg//StartVoteMessage
let $stopvote := $msg//StopVoteMessage
let $disabled := $msg//DisableServiceMessage

  where (coffee:get-tool($msg)="positionometerTool")
  return
  <item>{ 
  
  		<info name="software">{"coffee:softwarename"}</info>,
      <info name="tool">threadedChat</info>,
      <info name="time"><time><date>{coffee:get-timemillis($msg)}</date></time></info>,
    	<info name="src-anchor"><anchor>{
      	<doc>{ $t }</doc>,
      	<path>{jj:build-Path($msg)}</path>
    	}</anchor></info>,
      <info name="group">{coffee:get-group($msg)}</info>,
      <info name="step">{coffee:get-step($msg)}</info>,
      <info name="user">{coffee:get-username($msg)}</info>,
      if ($vote) then
      	(<info name="type">vote</info>,
      	<info name="content">{coffee:string($vote/@vote)}</info>
      )   
      else if ($startvote) then
      	(<info name="type">start vote</info>,
      	<info name="content"><text>{coffee:string($startvote/@question), ":",
      	coffee:string($startvote/@leftLabel), " or ",
      	coffee:string($startvote/@rightLabel)
      	}</text></info>
      ) 
      else if ($stopvote) then
      	(<info name="type">stop vote</info>,
      	<info name="content"><text>Vote ended</text></info>
      )
      else if ($disabled) then
      	(<info name="type">disable vote</info>,
      	<info name="content"><text>Vote disabled</text></info>
      )
      else
      	<info name="content">{"unknown positionometer message:", name($msg/ToolMessage/*),"please report this bug"}</info>
  }</item>
}</display>
